package com.xjh.him.adapter.emotion.unicode;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xjh.him.R;
import com.xjh.him.utils.EmotionUtils;
import com.xjh.him.utils.XToastUtils;

import java.util.List;

public class UnicodeEmotionAdapter extends BaseAdapter {
    private Context context;
    private List<String> emotionKeys;
    private int itemWidth;

    public UnicodeEmotionAdapter(Context context, List<String> emotionKeys, int itemWidth) {
        this.context = context;
        this.emotionKeys = emotionKeys;
        this.itemWidth = itemWidth;
    }

    @Override
    public int getCount() {
        return emotionKeys.size();
    }

    @Override
    public Object getItem(int position) {
        return emotionKeys.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView tv_emotion = new TextView(context);
        tv_emotion.setTextColor(R.color.white);
        tv_emotion.setTextSize(25);
        // 设置内边距
        tv_emotion.setPadding(itemWidth / 8, itemWidth / 8, itemWidth / 8, itemWidth / 8);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(itemWidth, itemWidth);
        tv_emotion.setLayoutParams(params);
        // 设置表情
        String key = emotionKeys.get(position);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            tv_emotion.setText(EmotionUtils.parseEmojiByUnicode(Integer.parseInt(key)));
        }else {
            XToastUtils.toast("build sdk version must greater 19, current is " + Build.VERSION.SDK_INT);
        }
        return tv_emotion;
    }
}
